//import angular core
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';

//import third package
import 'rxjs/add/operator/switchMap';

//import service
import { RoomTypeService } from '../../service/room-type.service';

//import class
import { RoomType } from '../../domain/roomType';
declare var layui: any;
@Component({
  selector: 'app-room-type-detail',
  templateUrl: './room-type-detail.component.html',
  styleUrls: ['./room-type-detail.component.css'],
  providers: [RoomTypeService]
})
export class RoomTypeDetailComponent implements OnInit {

  private roomType: RoomType;
  private roomTypeUpdate: RoomType;

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private roomTypeService: RoomTypeService,
    private location: Location
  ) { }

  ngOnInit() {

    layui.use(['element', 'layer', 'form'], function () {
      var element = layui.element;
      var layer = layui.layer;
      var form = layui.form;

      form.render(); //更新全部

      element.render('element');
    });


    this.route.params
      .switchMap((params: Params) => this.roomTypeService.getRoomTypeById(+params['id']))
      .subscribe(res => {
        this.roomType = res;
        this.roomTypeUpdate = res;
      });

  }

  updateRoomType(): void {
    this.roomTypeService.updateRoomType(this.roomTypeUpdate)
      .then(() => {
        this.goBack();
      })
  }

  reset(): void {
    this.roomTypeUpdate = this.roomType;
  }

  goBack(): void {
    this.location.back();
  }
}
